package indi.tshoiasc.simstr.commands;

import indi.tshoiasc.simstr.config.ConfigHolder;
import indi.tshoiasc.simstr.config.ConfigLoader;
import indi.tshoiasc.simstr.utils.GeneralOperate;
import indi.tshoiasc.simstr.utils.ItemOperate;
import indi.tshoiasc.simstr.utils.Menu;
import indi.tshoiasc.simstr.utils.PlayerOperate;
import indi.tshoiasc.simstr.utils.VaultOperate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class CommandStrength implements CommandExecutor {
	private static Map<Player, Long> map = new HashMap<Player, Long>();

	public boolean adminStrength(Player p, int lv) {
		if (!ConfigLoader.list.containsKey(Integer.valueOf(p.getItemInHand().getTypeId()))) {
			p.sendMessage("您手持的物品不是可强化武器");
		}
		ItemStack item = p.getItemInHand();
		ConfigHolder setting = (ConfigHolder) ConfigLoader.list.get(Integer.valueOf(item.getTypeId()));
		String name;

		if (p.getItemInHand().getItemMeta().getDisplayName() == null) {
			name = setting.getName();
		} else {
			name = item.getItemMeta().getDisplayName();
		}
		String broadcast = ConfigHolder.getBroadcast_content().replace("%playername%", p.getName())
				.replace("%level%", "" + lv).replace("%item%", name);

		Bukkit.broadcastMessage(broadcast);
		int index = ItemOperate.lookupLoreIndex(item, "§b[强化等级]:§6§l");
		StringBuffer sb = new StringBuffer("§b[强化等级]:§6§l");
		for (int i = 0; i < lv; i++) {
			sb.append("✡");
		}
		if (index == -1) {
			ItemOperate.addLore(item, "§b[强化等级]:§6§l✡");
		} else {
			ItemOperate.setLore(item, index, sb.toString());
		}
		return true;
	}

	public static boolean Strength(Player p, boolean luck, boolean safe) {
		if (!ConfigLoader.list.containsKey(Integer.valueOf(p.getItemInHand().getTypeId()))) {
			p.sendMessage("您手持的物品不是可强化武器");
			return false;
		}
		ConfigHolder setting = (ConfigHolder) ConfigLoader.list.get(Integer.valueOf(p.getItemInHand().getTypeId()));

		String name = p.getName();
		int money;
		if (luck) {
			money = ((Integer) setting.getLuck().get("costMoney")).intValue();
		} else {
			if (safe) {
				money = ((Integer) setting.getSafe().get("costMoney")).intValue();
			} else {
				money = ((Integer) setting.getNormal().get("costMoney")).intValue();
			}
		}
		ItemStack item = p.getItemInHand();
		if (!VaultOperate.has(name, money)) {
			p.sendMessage("§4您没有足够的金钱,强化所需§2" + money);
			return false;
		}
		int level = GeneralOperate.getLevel(item);
		if (level == 10) {
			p.sendMessage("§6武器已满级，无法继续强化。");
			return false;
		}
		if (luck) {
			if (!PlayerOperate.hasItem(p, (List<ItemStack>) setting.getLuck().get("costItem"))) {
				p.sendMessage("§4您没有足够的强化物品");
				return false;
			}
		} else if (safe) {
			if (!PlayerOperate.hasItem(p, (List<ItemStack>) setting.getSafe().get("costItem"))) {
				p.sendMessage("§4您没有足够的强化物品");
				return false;
			}
		} else if (!PlayerOperate.hasItem(p, (List<ItemStack>) setting.getNormal().get("costItem"))) {
			p.sendMessage("§4您没有足够的强化物品");
			return false;
		}
		if (map.get(p) == null) {
			map.put(p, Long.valueOf(System.currentTimeMillis()));
		} else {
			long cooldowna = System.currentTimeMillis();
			long cooldowne = map.get(p).longValue();
			if (cooldowna - cooldowne < ConfigHolder.getCooldown()) {
				p.sendMessage("§b强化冷却中……还需§2"
						+ Math.ceil((ConfigHolder.getCooldown() - (cooldowna - cooldowne)) / 1000.0D) + "§b秒");
				return false;
			}
			map.put(p, Long.valueOf(System.currentTimeMillis()));
		}
		if (luck) {
			PlayerOperate.takeItem(p, (List<ItemStack>) setting.getLuck().get("costItem"), true);
		} else if (safe) {
			PlayerOperate.takeItem(p, (List<ItemStack>) setting.getSafe().get("costItem"), true);
		} else {
			PlayerOperate.takeItem(p, (List<ItemStack>) setting.getNormal().get("costItem"), true);
		}
		int addRandom = 0;
		if (luck) {
			addRandom = ((Integer) setting.getLuck().get("addRandom")).intValue();
		}
		if (GeneralOperate
				.random(((Integer) ((List) setting.getNormal().get("random")).get(level)).intValue() + addRandom)) {
			p.sendMessage("§6强化成功,当前强化等级为§5§l" + (level + 1) + "§6级");
			p.getWorld().playEffect(p.getLocation(), ConfigHolder.successEffect, 5);
			String namename;
			if (item.getItemMeta().getDisplayName() == null) {
				namename = setting.getName();
			} else {
				namename = p.getItemInHand().getItemMeta().getDisplayName();
			}
			if (level + 1 >= ConfigHolder.Broadcast_level) {
				String broadcast = ConfigHolder.getBroadcast_content().replace("%playername%", p.getName())
						.replace("%level%", "" + (level + 1)).replace("%item%", namename);
				Bukkit.broadcastMessage(broadcast);
			}
			GeneralOperate.setLevel(item, level + 1);
			return true;
		}
		if ((level >= 7) && (!safe)) {
			p.setItemInHand(new ItemStack(Material.AIR));
			p.sendMessage("§4强化失败,物品已损坏");
			p.getWorld().playEffect(p.getLocation(), ConfigHolder.failEffect, 5);
			return false;
		}
		if (safe) {
			p.sendMessage("§4强化失败,保护强化不降低等级");
			p.getWorld().playEffect(p.getLocation(), ConfigHolder.failEffect, 5);
			return false;
		}
		p.sendMessage("§4强化失败,强化等级下降为§5" + (level - 1) + "§4级");
		GeneralOperate.setLevel(item, level - 1);
		p.getWorld().playEffect(p.getLocation(), ConfigHolder.failEffect, 5);
		return false;
	}

	@SuppressWarnings("unchecked")
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		if (cmd.getName().equalsIgnoreCase("QH")) {
			if (!(sender instanceof Player)) {
				sender.sendMessage("禁止用控制台发送此指令");
				return true;
			}
			if ((!sender.hasPermission("QH.use")) && (!sender.isOp())) {
				sender.sendMessage("§4权限不足");
			}
			if (args.length == 0) {
				sender.sendMessage(Menu.getMenu());
				return true;
			}
			if ((args.length == 2) && (args[0].equalsIgnoreCase("admin"))) {
				if ((!sender.isOp()) && (!sender.hasPermission("QH.admin"))) {
					sender.sendMessage("§4权限不足");
					return true;
				}
				adminStrength((Player) sender, Integer.valueOf(args[1]).intValue());
				return true;
			}
			if ((args.length == 1) && (args[0].equalsIgnoreCase("cost"))) {
				if (!(sender instanceof Player)) {
					return false;
				}
				Player p = (Player) sender;
				if (!ConfigLoader.list.containsKey(Integer.valueOf(p.getItemInHand().getTypeId()))) {
					p.sendMessage("您手持的物品不是可强化武器");
					return false;
				}
				ConfigHolder ch = (ConfigHolder) ConfigLoader.list.get(Integer.valueOf(p.getItemInHand().getTypeId()));
				List<String> info = new ArrayList<String>();

				info.add("§2普通强化所需金钱:§e " + (Integer) ch.getNormal().get("costMoney"));
				StringBuffer sb = new StringBuffer();
				for (ItemStack im : (List<ItemStack>) (ch.getNormal().get("costItem"))) {
					if (im.hasItemMeta()) {
						sb.append(" " + im.getItemMeta().getDisplayName() + "×" + im.getAmount());
					} else {
						sb.append(" " + im.getType().toString() + "×" + im.getAmount());
					}
				}
				info.add("§2普通强化所需物品:" + sb.toString());
				info.add("§2幸运强化所需金钱:§e " + (Integer) ch.getLuck().get("costMoney"));
				StringBuffer sb2 = new StringBuffer();
				for (ItemStack im : (List<ItemStack>) ch.getLuck().get("costItem")) {
					if (im.hasItemMeta()) {
						sb2.append(" " + im.getItemMeta().getDisplayName() + "×" + im.getAmount());
					} else {
						sb2.append(" " + im.getType().toString() + "×" + im.getAmount());
					}
				}
				info.add("§2幸运强化所需物品:" + sb2.toString());
				info.add("§2保护强化所需金钱:§e " + (Integer) ch.getSafe().get("costMoney"));
				StringBuffer sb3 = new StringBuffer();
				for (ItemStack im : (List<ItemStack>) ch.getSafe().get("costItem")) {
					if (im.hasItemMeta()) {
						sb3.append(" " + im.getItemMeta().getDisplayName() + "×" + im.getAmount());
					} else {
						sb3.append(" " + im.getType().toString() + "×" + im.getAmount());
					}
				}
				info.add("§2保护强化所需物品:" + sb3.toString());
				for (int i = 0; i < info.size(); i++) {
					p.sendMessage(info.get(i));
				}
			}
			if ((args.length == 1) && (args[0].equalsIgnoreCase("normal"))) {
				Strength((Player) sender, false, false);
				return true;
			}
			if ((args.length == 1) && (args[0].equalsIgnoreCase("luck"))) {
				Strength((Player) sender, true, false);
				return true;
			}
			if ((args.length == 1) && (args[0].equalsIgnoreCase("safe"))) {
				Strength((Player) sender, false, true);
				return true;
			}
			if ((args.length == 3) && (args[0].equalsIgnoreCase("give"))) {
				Player p = (Player) sender;
				if ((!sender.isOp()) && (!sender.hasPermission("QH.admin"))) {
					sender.sendMessage("§4权限不足");
					return true;
				}
				ItemStack it = (ItemStack) ConfigLoader.itemMap.get(args[1]);
				if ((it == null) || (it.getType() == Material.AIR)) {
					p.sendMessage("§c没找到这个物品哦");
					return true;
				}
				it.setAmount(Integer.valueOf(args[2]).intValue());
				p.getInventory().addItem(new ItemStack[] { it });

				p.sendMessage("§2§l" + args[2] + "个" + args[1] + "已送达");
				return true;
			}
		}
		return false;
	}
}
